package com.fxnotes.common.ui.components;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.BorderPaneBuilder;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.stage.Modality;
import javafx.stage.Stage;

public class ModalDialog {

    private Stage stage;
    private BorderPane borderPane;
    private HBox buttonsPanel;

    protected ModalDialog(ModalDialogBuilder builder) {
        createStage(builder);
        createPane(builder);
        createButtons(builder);
        
        wireUpComponents();
    }

    private void createStage(ModalDialogBuilder builder) {
        stage = new Stage();
        stage.initModality(Modality.WINDOW_MODAL);
        stage.initOwner(builder.getOwner());
        stage.setTitle(builder.getTitle());
    }

    private void createPane(ModalDialogBuilder builder) {
        borderPane = BorderPaneBuilder.create()
                .padding(new Insets(15))
                .build();

        if (builder.getOwner() != null) {
            borderPane.setMaxWidth(builder.getOwner().getWidth());
            borderPane.setMaxHeight(builder.getOwner().getHeight());
        }

        if (builder.getWidth() != null) {
            borderPane.setPrefWidth(builder.getWidth());
        }

        if (builder.getHeight() != null) {
            borderPane.setPrefHeight(builder.getHeight());
        }

        if (builder.getContent() != null) {
            borderPane.setLeft(builder.getContent());
        }
    }    
    
    private void createButtons(ModalDialogBuilder builder) {
        buttonsPanel = HBoxBuilder.create()
                .alignment(Pos.TOP_RIGHT)
                .padding(new Insets(5))
                .spacing(10)
                .build();

        if (builder.getConfirmationButton() != null) {
            buttonsPanel.getChildren().add(createButton(builder.getConfirmationButton()));
        }

        buttonsPanel.getChildren().add(createButton(builder.getCancelButton()));
    }
    
    private void wireUpComponents() {
        borderPane.setBottom(buttonsPanel);

        Scene scene = new Scene(borderPane);
        stage.setScene(scene);
    }    

    public void show() {
        if (stage != null) {
            stage.show();
        }
    }

    private Button createButton(final ButtonInfo buttonInfo) {
        Button button = new Button(buttonInfo.getName());
        button.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                stage.hide();
                if (buttonInfo.getHandler() != null) {
                    buttonInfo.getHandler().handle(event);
                }
            }
        });
        
        return button;
    }
}
